package com.aviation.sell_ticket.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.aviation.sell_ticket.mapper.ThemeMapper;
import com.aviation.sell_ticket.pojo.Aviation_city;
import com.aviation.sell_ticket.pojo.Aviation_theme;
import com.aviation.sell_ticket.service.ThemeService;
import com.aviation.sell_ticket.tool.RedisUtil;
import com.aviation.sell_ticket.tool.ResultEnum;
import com.aviation.sell_ticket.tool.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ThemeServiceImpl implements ThemeService {

    @Resource
    ThemeMapper themeMapper;

    @Resource
    RedisUtil redis;

    /**
     * 取出所有主题信息
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> wholeTheme() {
        JSONArray themeArray=null;
        try {
            //从缓存中提取数据
            String ThemeData=redis.getRedis("Theme");
            //判断缓存是否存在
            if (null == ThemeData) {
                //从数据库提取数据
                List<Aviation_theme> themeList = themeMapper.wholeTheme();
                //转换json
                themeArray = JSONArray.parseArray(JSON.toJSONString(themeList));
                //添加数据进缓存
                redis.setRidesTime("Theme",themeArray.toJSONString(),(long) (60 * 60), TimeUnit.SECONDS);
            }else {
                //缓存中有数据，转成json
                List<Aviation_theme> themeList= JSONArray.parseArray(ThemeData,Aviation_theme.class);
                themeArray = JSONArray.parseArray(JSON.toJSONString(themeList));
            }
        }catch (Exception e){
            return ResultUtil.beDefeated(ResultEnum.UNKNOWN_ERROR);
        }
        return ResultUtil.success(ResultEnum.SUCCESS, themeArray);
    }
}
